import publicForm from "@/components/public/el-forms.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import TableData from "@/components/public/el-table-pagination"; //主页面列表

import publicForm1 from "@/components/materialDataManage/fromCom.vue"; //弹框form

import {

    getInstrumentInfoList, //列表页接口
    getFunctionRoomList, //功能室
} from "@/api/BackStage/materialQualityPage/request.js";

export default {
    props: [
        "dialogVisible",
    ],
    data() {
        return {
            visible: "",
            queryCriteriaData: {}, //分页
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表
            materialInfo_config: formJs.materialInfo_config,
            // 列表
            getList: {
                fCompanyid: "", //左侧选中组织机构id
                keyType: "",
                keyWord: "", //上方查询条件样品名称
                page: 1, //当前页码
                pageSize: 10, //每页显示条数
                typ: ""
            },
            loading: false,
            materialData: [], //种类数据
            tableData: [],
            title: null,
            isLock: false,
            dialogHeight: null,
            activeIndex: "1",
            btnShow: true,
            currentRow: null,
            types: null,
        };
    },
    components: {
        publicForm,

        TableData,
        publicForm1
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.btnShow = true
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.getList.fCompanyid = this.userInfo.companyId
        this.getHeight();
        this.yj_searchForm(this.getList)
        this.getFunctionRoom(this.getList)
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.5 + "px";
        },
        closeDialog1() {
            this.$emit("close1");
        },

        rowChange1(val) {
            this.getList.fId = val.fVarietyid;
            this.types = val
            this.yj_searchForm(this.getList)
        },
        // 点击材料
        rowChange(val) {
            this.currentRow = val
            // this.dataFid = val.fId;
        },
        // 获取列表数据
        getFunctionRoom(data) {
            var obj = {
                fCompanyid: data.fCompanyid, //左侧选中组织机构id
                page: 1, //当前页码
                typ: "",
                pageSize: 500, //每页显示条数
                
                keyWord: "", //上方查询条件样品名称
            }
            this.loading = true;
            var that = this;
            getFunctionRoomList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        this.searchObj[0].options=[{
                            label: "请选择",
                            value:""
                        }]
                        var dt=this.uniqueBy(res.data.pageInfo.list, 'functionRoom')
                        dt.map(item=>{
                            this.searchObj[0].options.push({
                                lable:item.functionRoom,
                                value:item.functionRoom
                            })
                        })
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        uniqueBy(array, key) {
            const seen = new Set();
            return array.filter((item) => {
              const keyValue = item[key];
              return seen.has(keyValue) ? false : seen.add(keyValue);
            });
          },
        // 获取列表数据
        yj_searchForm(data) {
            var obj = {
                fCompanyid: data.fCompanyid, //左侧选中组织机构id
                page: data.page, //当前页码
                pageSize: data.pageSize, //每页显示条数
                keyWord: data.keyWord, //上方查询条件样品名称
                typ: data.typ,
                keyType: data.keyType
            }
            this.loading = true;
            var that = this;
            getInstrumentInfoList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },

        yj_saveForm() {
           
                if (this.currentRow != null) {
                    this.$emit('getMateral', this.currentRow)
                    this.visible = false
                } else {
                    this.$message.error('请先选择材料');
                }
            
        },

    },
};